package net.cjw.initsb.controller.test;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import springfox.documentation.annotations.ApiIgnore;
@ApiIgnore
@Controller
public class ShiroDemoController {

	@GetMapping("loginError")
	@ResponseBody
	public String loginError() {
		return "你没有登录,无法访问";
	}

	@GetMapping("permissionError")
	@ResponseBody
	public String permissionError() {
		return "你没有权限,无法访问";
	}

	@GetMapping("logout")
	@ResponseBody
	public String logout() {
		Subject subject = SecurityUtils.getSubject();
		System.out.println("logout() sessionid: " + subject.getSession().getId());
		subject.logout();
		return "登出成功";
	}

	@GetMapping("login")
	@ResponseBody
	public String login() {
		// 假设是前端传过来的账号密码
		String username = "111";
		String password = "222";

		UsernamePasswordToken upt = new UsernamePasswordToken(username, password);
		Subject subject = SecurityUtils.getSubject();
		System.out.println("login() sessionid: " + subject.getSession().getId());
		try {
			System.out.println("upt: " + upt);
			subject.login(upt);
		} catch (AuthenticationException e) {
			// e.printStackTrace();
			return "登录失败";
		}
		return "登录成功";
	}

	@GetMapping("other")
	@ResponseBody
	public String other() {
		Subject subject = SecurityUtils.getSubject();
		String username = subject.getPrincipal().toString();
		System.out.println("other() sessionid: " + subject.getSession().getId());
		System.out.println("username: " + username);
		return "访问资源成功 访问用户: " + username;
	}

	@GetMapping("other2")
	@ResponseBody
	public String other2() {
		Subject subject = SecurityUtils.getSubject();
		String username = subject.getPrincipal().toString();
		System.out.println("other2() sessionid: " + subject.getSession().getId());
		System.out.println("username: " + username);
		return "访问资源成功 访问用户: " + username;
	}

	@GetMapping("other3")
	@ResponseBody
	public String other3() {
		Subject subject = SecurityUtils.getSubject();
		String username = subject.getPrincipal().toString();
		System.out.println("other2() sessionid: " + subject.getSession().getId());
		System.out.println("username: " + username);
		return "访问资源成功 访问用户: " + username;
	}

	@GetMapping("other4")
	@ResponseBody
	public String other4() {
		Subject subject = SecurityUtils.getSubject();
		String username = subject.getPrincipal().toString();
		System.out.println("other2() sessionid: " + subject.getSession().getId());
		System.out.println("username: " + username);
		return "访问资源成功 访问用户: " + username;
	}

	@GetMapping("other5")
	@ResponseBody
	public String other5() {
		Subject subject = SecurityUtils.getSubject();
		String username = subject.getPrincipal().toString();
		System.out.println("other2() sessionid: " + subject.getSession().getId());
		System.out.println("username: " + username);
		return "访问资源成功 访问用户: " + username;
	}
	@GetMapping("other6")
	@ResponseBody
	public String other6() {
		Subject subject = SecurityUtils.getSubject();
		String username = subject.getPrincipal().toString();
		System.out.println("other2() sessionid: " + subject.getSession().getId());
		System.out.println("username: " + username);
		return "访问资源成功 访问用户: " + username;
	}
	@GetMapping("other7")
	@ResponseBody
	public String other7() {
		Subject subject = SecurityUtils.getSubject();
		String username = subject.getPrincipal().toString();
		System.out.println("other2() sessionid: " + subject.getSession().getId());
		System.out.println("username: " + username);
		return "访问资源成功 访问用户: " + username;
	}

}
